<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>TestForANSIForScope &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="TestForANSIStreamHeaders" href="TestForANSIStreamHeaders.html" />
    <link rel="prev" title="SelectLibraryConfigurations" href="SelectLibraryConfigurations.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="TestForANSIStreamHeaders.html" title="TestForANSIStreamHeaders"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="SelectLibraryConfigurations.html" title="SelectLibraryConfigurations"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">TestForANSIForScope</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="testforansiforscope">
<span id="module:TestForANSIForScope"></span><h1>TestForANSIForScope<a class="headerlink" href="#testforansiforscope" title="Link to this heading">¶</a></h1>
<p>This module checks whether the <code class="docutils literal notranslate"><span class="pre">CXX</span></code> compiler restricts the scope of variables
declared in a for-init-statement to the loop body.</p>
<p>Load this module in a CMake project with:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">TestForANSIForScope</span><span class="nf">)</span>
</pre></div>
</div>
<p>In early C++ (pre-C++98), variables declared in <code class="docutils literal notranslate"><span class="pre">for(&lt;init-statement&gt;</span> <span class="pre">...)</span></code>
could remain accessible outside the loop after its body (<code class="docutils literal notranslate"><span class="pre">for()</span> <span class="pre">{</span> <span class="pre">&lt;body&gt;</span> <span class="pre">}</span></code>).</p>
<p>This module defines the following cache variable:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_NO_ANSI_FOR_SCOPE</span></code></dt><dd><p>A cache variable containing the result of the check.  It will be set to value
<code class="docutils literal notranslate"><span class="pre">0</span></code> if the for-init-statement has restricted scope (<code class="docutils literal notranslate"><span class="pre">C++</span> <span class="pre">98</span></code> and newer),
and to value <code class="docutils literal notranslate"><span class="pre">1</span></code> if not (<code class="docutils literal notranslate"><span class="pre">ANSI</span> <span class="pre">C++</span></code>).</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>As of the <code class="docutils literal notranslate"><span class="pre">C++</span> <span class="pre">98</span></code> standard, variables declared in a for-init-statement are
restricted to the loop body, making this behavior obsolete.</p>
</div>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>Including this module will check the <code class="docutils literal notranslate"><span class="pre">for()</span></code> loop scope behavior and define
the <code class="docutils literal notranslate"><span class="pre">CMAKE_NO_ANSI_FOR_SCOPE</span></code> cache variable:</p>
<div class="literal-block-wrapper docutils container" id="id1">
<div class="code-block-caption"><span class="caption-text"><code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code></span><a class="headerlink" href="#id1" title="Link to this code">¶</a></div>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">TestForANSIForScope</span><span class="nf">)</span>
<span class="nf">file(</span>
<span class="w">  </span><span class="no">CONFIGURE</span>
<span class="w">  </span><span class="no">OUTPUT</span><span class="w"> </span><span class="nb">config.h</span>
<span class="w">  </span><span class="no">CONTENT</span><span class="w"> </span><span class="s">&quot;#cmakedefine CMAKE_NO_ANSI_FOR_SCOPE&quot;</span>
<span class="nf">)</span>
</pre></div>
</div>
</div>
<p>which can be then used in a C++ program:</p>
<div class="literal-block-wrapper docutils container" id="id2">
<div class="code-block-caption"><span class="caption-text"><code class="docutils literal notranslate"><span class="pre">example.cxx</span></code></span><a class="headerlink" href="#id2" title="Link to this code">¶</a></div>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;config.h&quot;</span>

<span class="cp">#ifdef CMAKE_NO_ANSI_FOR_SCOPE</span>
<span class="cp">#  define for if(false) {} else for</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</div>
</section>
<section id="see-also">
<h2>See Also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<ul class="simple">
<li><p>The <span class="target" id="index-0-module:CMakeBackwardCompatibilityCXX"></span><a class="reference internal" href="CMakeBackwardCompatibilityCXX.html#module:CMakeBackwardCompatibilityCXX" title="CMakeBackwardCompatibilityCXX"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">CMakeBackwardCompatibilityCXX</span></code></a> module.</p></li>
</ul>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">TestForANSIForScope</a><ul>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#see-also">See Also</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="SelectLibraryConfigurations.html"
                          title="previous chapter">SelectLibraryConfigurations</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="TestForANSIStreamHeaders.html"
                          title="next chapter">TestForANSIStreamHeaders</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/TestForANSIForScope.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="TestForANSIStreamHeaders.html" title="TestForANSIStreamHeaders"
             >next</a> |</li>
        <li class="right" >
          <a href="SelectLibraryConfigurations.html" title="SelectLibraryConfigurations"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">TestForANSIForScope</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>